﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Media;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectSound;

namespace Tank
{
    public class PlayMusic
    {
        //public static uint SND_ASYNC = 0x0001;
        //public static uint SND_FILENAME = 0x00020000;
        //[DllImport("winmm.dll")]
        //public static extern uint mciSendString(string lpstrCommand,
        //string lpstrReturnString, uint uReturnLength, uint hWndCallback);

        /// <summary>
        /// 背景音乐对象
        /// </summary>
        private static SoundPlayer sp;

        /// <summary>
        /// 播放开始背景音乐
        /// </summary>
        public static void startBackMusic()
        {
            sp = new System.Media.SoundPlayer(AppDomain.CurrentDomain.BaseDirectory + @"\Music\start.wav");
            sp.PlayLooping();
        }

        /// <summary>
        /// 停止播放背景音乐
        /// </summary>
        public static void stopBackMusic()
        {
            sp.Stop();
        }


        /// <summary>
        /// 播放选择时音效
        /// </summary>
        public static void selectMusic()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + @"\Music\fire.wav";
            Device secDev = new Device();
            SecondaryBuffer secBuffer;//缓冲区对象    
            secDev.SetCooperativeLevel(Data.indexFrmIntPtr, CooperativeLevel.Normal);//设置设备协作级别              
            secBuffer = new SecondaryBuffer(path, secDev);//创建辅助缓冲区              
            secBuffer.Play(0, BufferPlayFlags.Default);//设置缓冲区为默认播放 
        }

        /// <summary>
        /// 玩家开火
        /// </summary>
        /// <param name="ip"></param>
        public static void fireMusic()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + @"\Music\fire.wav";
            Device secDev = new Device();
            SecondaryBuffer secBuffer;//缓冲区对象    
            secDev.SetCooperativeLevel(Data.indexFrmIntPtr, CooperativeLevel.Normal);//设置设备协作级别              
            secBuffer = new SecondaryBuffer(path, secDev);//创建辅助缓冲区              
            secBuffer.Play(0, BufferPlayFlags.Default);//设置缓冲区为默认播放 
        }

        /// <summary>
        /// NPC被消灭
        /// </summary>
        /// <param name="ip"></param>
        public static void blastMusic()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + @"\Music\blast.wav";
            Device secDev = new Device();
            SecondaryBuffer secBuffer;//缓冲区对象    
            secDev.SetCooperativeLevel(Data.indexFrmIntPtr, CooperativeLevel.Normal);//设置设备协作级别              
            secBuffer = new SecondaryBuffer(path, secDev);//创建辅助缓冲区              
            secBuffer.Play(0, BufferPlayFlags.Default);//设置缓冲区为默认播放 
        }

        /// <summary>
        /// 播放选择时音效
        /// </summary>
        //public static void selectMusic()
        //{
        //    string path = AppDomain.CurrentDomain.BaseDirectory + @"Music\fire.wav";
        //    mciSendString(@"close media", null, 0, 0);
        //    mciSendString("open \"" + path + "\" type mpegvideo alias media", null, 0, 0);
        //    mciSendString("play media notify", null, 0, 0);
        //    //SoundPlayer sp = new System.Media.SoundPlayer(AppDomain.CurrentDomain.BaseDirectory + @"\Music\fire.wav");
        //    //sp.Play();
        //}

        /// <summary>
        /// 播放指定位置音效
        /// </summary>
        /// <param name="path">文件位置</param>
        //public static void palyMusic(string path)
        //{
        //    mciSendString(@"close media", null, 0, 0);
        //    mciSendString("open \"" + path + "\" type mpegvideo alias media", null, 0, 0);
        //    mciSendString("play media notify", null, 0, 0);
        //}


    }
}
